package com.ziyun.erp.common.utils;

import com.ziyun.erp.common.e.order.*;
import com.ziyun.erp.modules.member.entity.UserBaseInfoEntity;
import com.ziyun.erp.modules.member.service.UserBaseInfoService;
import com.ziyun.erp.modules.order.entity.ErpOrderEntity;
import com.ziyun.erp.modules.order.entity.OrderInfoEntity;
import com.ziyun.erp.modules.order.entity.OrderItemEntity;
import com.ziyun.erp.modules.order.entity.UserPaymentEntity;
import com.ziyun.erp.modules.order.service.UserPaymentService;
import com.ziyun.erp.modules.production.entity.ErpOrderItemEntity;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.supp.service.SellerInfoService;
import io.renren.common.utils.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guizhao
 * @Title: ErpOrderUtils
 * @ProjectName renren-security
 * @Description: 订单工具类
 * @date 2018-08-0413:27
 */
public class ErpOrderUtils {
    private static Logger logger = LoggerFactory.getLogger(ErpOrderUtils.class);
    /**
     *  订单来源：紫云网
     */
    private static final int SOURCE_ZIYUN = 1;

    /**
     *  订单来源：线下门店
     */
    private static final int SOURCE_OFFLINE = 2;

    /**
     * 会员信息
     */
    private static UserBaseInfoService userBaseInfoService;

    /**
     * 第三方图文店
     */
    private static SellerInfoService sellerInfoService;

    /**
     * 用户支付
     */
    private static UserPaymentService userPaymentService;

    static {
        userBaseInfoService = SpringContextUtils.getBean("userBaseInfoService",UserBaseInfoService.class);
        sellerInfoService = SpringContextUtils.getBean("sellerInfoService",SellerInfoService.class);
        userPaymentService = SpringContextUtils.getBean("userPaymentService",UserPaymentService.class);
    }

    /**
     * 紫云网订单==》erp订单
     * @param orderInfoEntity
     * @return ErpOrderEntity
     */
    public static ErpOrderEntity ziyunOrderToErp(OrderInfoEntity orderInfoEntity){
        ErpOrderEntity erpOrderEntity = new ErpOrderEntity();
        // 订单号
        erpOrderEntity.setOrderNo(orderInfoEntity.getOrderId());
        // 会员ID
        erpOrderEntity.setMemberId(orderInfoEntity.getUserId());
        // 会员名
        UserBaseInfoEntity userBaseInfoEntity = userBaseInfoService.getEntityByuserId(orderInfoEntity.getUserId());
        if(userBaseInfoEntity == null){
            return null;
        }
        erpOrderEntity.setMemberName(userBaseInfoEntity.getName());
        // 订单来源
        erpOrderEntity.setOrderSource(OrderSourceEnum.ZIYUN.toInt());
        // 第三方类型
        if(orderInfoEntity.getSupplierId() == 1){ // 紫云自营
            erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.ZIYUN.toInt());
        } else {
            SellerInfoEntity sellerInfoEntity = sellerInfoService.selectById(orderInfoEntity.getSupplierId());
            if(sellerInfoEntity.getSelf().intValue() == 1){ // 自营门店
                erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.ZIYUN_MERCHANT.toInt());
            } else {
                if ("0".equals(sellerInfoEntity.getSellerType())){ // 商户
                    erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.MERCHANT.toInt());
                }else { // 工厂
                    erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.FACTOEY.toInt());
                }
            }
        }
        erpOrderEntity.setThirdMerchantId(orderInfoEntity.getSupplierId());
        // 源订单编号
        erpOrderEntity.setOriginalOrderId(Long.valueOf(orderInfoEntity.getOrderId()));
        // 生产状态
        if("finish".equals(orderInfoEntity.getStatus())){
            erpOrderEntity.setProductionStatus(ProductionStatusEnum.FINISH.toInt());
        }else if("dead".equals(orderInfoEntity.getStatus())){
            erpOrderEntity.setProductionStatus(ProductionStatusEnum.CANCELLED.toInt());
        }else{
            // 自营门店
            if(erpOrderEntity.getThirdMerchantType() == ThirdMerchantTypeEnum.ZIYUN_MERCHANT.toInt()){
                erpOrderEntity.setProductionStatus(ProductionStatusEnum.NEW_ORDER.toInt());
            }else if(erpOrderEntity.getThirdMerchantType() == ThirdMerchantTypeEnum.MERCHANT.toInt()){ // 第三方门店
                erpOrderEntity.setProductionStatus(ProductionStatusEnum.UN_AUDITED.toInt());
            }
        }
        // 付款状态
        erpOrderEntity.setPayStatus(Integer.parseInt(orderInfoEntity.getPayStatus()));
        // 发货状态
        erpOrderEntity.setShipStatus(Integer.parseInt(orderInfoEntity.getShipStatus()));
        // 收货状态
        erpOrderEntity.setReceivedStatus(orderInfoEntity.getReceivedStatus()?1:0);
        // 下单来源
        erpOrderEntity.setSource(orderInfoEntity.getSource());
        // 商品总值
        erpOrderEntity.setTotalAmount(orderInfoEntity.getTotalAmount());
        // 配送费用
        erpOrderEntity.setCostFreight(orderInfoEntity.getCostFreight());
        // 调价
        erpOrderEntity.setDiscount(orderInfoEntity.getDiscount());
        // 商品促销优惠
        erpOrderEntity.setPmtGoods(orderInfoEntity.getPmtGoods());
        // 优惠卷优惠
        erpOrderEntity.setPmtOrder(orderInfoEntity.getPmtOrder());
        // 随机立减
        erpOrderEntity.setPmtRandom(orderInfoEntity.getPmtRandom());
        // 订单应付金额
        erpOrderEntity.setPayableAmount(orderInfoEntity.getPayableAmount());
        // 订单已支付金额
        erpOrderEntity.setPayed(orderInfoEntity.getPayed());
        // 下单时间
        erpOrderEntity.setOrderCreatetime(orderInfoEntity.getCreatetime());
        // 收货时间
        erpOrderEntity.setReceivedTime(orderInfoEntity.getReceivedTime());
        // 支付信息
        UserPaymentEntity userPaymentEntity = userPaymentService.getPaySuccLastEntityByOrderId(orderInfoEntity.getOrderId());
        if (userPaymentEntity != null) {
            erpOrderEntity.setPayment(userPaymentEntity.getPayName());
            erpOrderEntity.setPaymentTime(userPaymentEntity.getTPayed());
            erpOrderEntity.setPaymentId(userPaymentEntity.getPaymentId());
        }
        // 订单类型
        if("paper".equals(orderInfoEntity.getPromotionType())){
            erpOrderEntity.setOrderType(OrderTypeEnum.PAPER.toInt());
        } else {
            erpOrderEntity.setOrderType(OrderTypeEnum.GENERAL.toInt());
        }
        // 订单总重量
        erpOrderEntity.setWeight(orderInfoEntity.getWeight());
        // IP地址
        erpOrderEntity.setIp(orderInfoEntity.getIp());
        // 送货方式
        if(orderInfoEntity.getPickUp()){
            erpOrderEntity.setSendType(SendTypeEnum.PICK_UP.toInt());
        } else {
            erpOrderEntity.setSendType(SendTypeEnum.SEND.toInt());
        }
        // 收货人
        erpOrderEntity.setShipName(orderInfoEntity.getShipName());
        // 收货区域id
        erpOrderEntity.setShipAreaId(orderInfoEntity.getShipAreaId());
        // 收货地区
        erpOrderEntity.setShipArea(orderInfoEntity.getShipArea());
        // 收货地址
        erpOrderEntity.setShipAddr(orderInfoEntity.getShipAddr());
        // 收货人邮编
        erpOrderEntity.setShipZip(orderInfoEntity.getShipZip());
        // 收货电话
        erpOrderEntity.setShipTel(orderInfoEntity.getShipTel());
        // 配送时间
        erpOrderEntity.setShipTime(orderInfoEntity.getShipTime());
        // 收货人手机
        erpOrderEntity.setShipMobile(orderInfoEntity.getShipMobile());
        // 推荐码
        erpOrderEntity.setReferralsCode(orderInfoEntity.getReferralsCode());
        // 客户留言
        erpOrderEntity.setMemo(orderInfoEntity.getMemo());

        return erpOrderEntity;
    }

    /**
     * 紫云网订单商品明细==》erp订单商品明细
     * @param orderItemList
     * @param orderNo
     * @return
     */
    public static List<ErpOrderItemEntity> ziyunOrderItemToErp(List<OrderItemEntity> orderItemList, String orderNo) {
        List<ErpOrderItemEntity> erpOrderItemEntityList = new ArrayList<>();
        for (OrderItemEntity orderItemEntity:orderItemList){
            ErpOrderItemEntity erpOrderItemEntity = new ErpOrderItemEntity();
            erpOrderItemEntity.setOrderNo(orderNo);
            erpOrderItemEntity.setProductId(orderItemEntity.getProductId());
            erpOrderItemEntity.setGoodsId(orderItemEntity.getGoodsId().intValue());
            erpOrderItemEntity.setName(orderItemEntity.getName());
            erpOrderItemEntity.setPrice(orderItemEntity.getPrice());
            erpOrderItemEntity.setAmount(orderItemEntity.getAmount());
            erpOrderItemEntity.setSharePrice(orderItemEntity.getSharePrice());
            erpOrderItemEntity.setWeight(orderItemEntity.getWeight());
            erpOrderItemEntity.setNums(orderItemEntity.getNums());
            erpOrderItemEntity.setUnit(orderItemEntity.getUnit());
            erpOrderItemEntity.setAddon(orderItemEntity.getAddon());
            erpOrderItemEntity.setItemType(orderItemEntity.getItemType());
            erpOrderItemEntity.setAddcartBuyType(orderItemEntity.getGoodsType());
            erpOrderItemEntity.setPaperUnit(orderItemEntity.getPaperUnit());
            erpOrderItemEntity.setPaperSize(orderItemEntity.getPaperSize());
            erpOrderItemEntity.setCalculationPriceProcess(orderItemEntity.getCalculationPriceProcess());
            erpOrderItemEntity.setOriginalItemId(orderItemEntity.getItemId());
            erpOrderItemEntityList.add(erpOrderItemEntity);
        }
        return erpOrderItemEntityList;
    }


    /**
     * erp==》订单紫云网订单
     * @param erpOrderEntity
     * @return orderInfoEntity
     */
    public static OrderInfoEntity erpToZiyunOrder(ErpOrderEntity erpOrderEntity){
        OrderInfoEntity orderInfoEntity=new OrderInfoEntity();
        // 订单号
        orderInfoEntity.setOrderId(erpOrderEntity.getOrderNo());
        // 会员ID
        orderInfoEntity.setUserId(erpOrderEntity.getMemberId());
        //绑定供应商ID
        orderInfoEntity.setSupplierId(erpOrderEntity.getThirdMerchantId());
        //父订单号
        orderInfoEntity.setParentOrderId(Long.valueOf(erpOrderEntity.getOrderNo()));
        // 生产状态
        if(ProductionStatusEnum.FINISH.toInt()==erpOrderEntity.getProductionStatus()){
            orderInfoEntity.setStatus(OrderInfoStatusEnum.FINISH.toCode());
        }else if(ProductionStatusEnum.CANCELLED.toInt()==erpOrderEntity.getProductionStatus()){
            orderInfoEntity.setStatus(OrderInfoStatusEnum.DEAD.toCode());
        }
        // 付款状态
        orderInfoEntity.setPayStatus( erpOrderEntity.getPayStatus().toString());
        // 发货状态
        orderInfoEntity.setShipStatus(erpOrderEntity.getShipStatus().toString());
        // 收货状态
        orderInfoEntity.setReceivedStatus(erpOrderEntity.getReceivedStatus()==1);
        // 下单来源
        orderInfoEntity.setSource(erpOrderEntity.getSource());
        // 商品总值
        orderInfoEntity.setTotalAmount(erpOrderEntity.getTotalAmount());
        // 配送费用
        orderInfoEntity.setCostFreight(erpOrderEntity.getCostFreight());
        // 调价
        orderInfoEntity.setDiscount(erpOrderEntity.getDiscount());
        // 商品促销优惠
        orderInfoEntity.setPmtGoods(erpOrderEntity.getPmtGoods());
        // 优惠卷优惠
        orderInfoEntity.setPmtOrder(erpOrderEntity.getPmtOrder());
        // 随机立减
        orderInfoEntity.setPmtRandom(erpOrderEntity.getPmtRandom());
        // 订单应付金额
        orderInfoEntity.setPayableAmount(erpOrderEntity.getPayableAmount());
        // 订单已支付金额
        orderInfoEntity.setPayed(erpOrderEntity.getPayed());
        // 下单时间
        orderInfoEntity.setCreatetime(erpOrderEntity.getOrderCreatetime());
        // 收货时间
        orderInfoEntity.setReceivedTime(erpOrderEntity.getReceivedTime());
        // 支付信息
        UserPaymentEntity userPaymentEntity = userPaymentService.getPaySuccLastEntityByOrderId(orderInfoEntity.getOrderId());
        if (userPaymentEntity != null) {
            orderInfoEntity.setPayment(userPaymentEntity.getPayName());
            orderInfoEntity.setPaymentTime(userPaymentEntity.getTPayed());
        }
        // 订单类型
        if(OrderTypeEnum.GENERAL.toInt()==erpOrderEntity.getOrderType()){
            orderInfoEntity.setPromotionType("normal");
        } else {
            orderInfoEntity.setPromotionType("paper");
        }
        // 订单总重量
        orderInfoEntity.setWeight(erpOrderEntity.getWeight());
        // IP地址
        orderInfoEntity.setIp(erpOrderEntity.getIp());
        // 送货方式
        orderInfoEntity.setPickUp(erpOrderEntity.getSendType()==SendTypeEnum.PICK_UP.toInt());
        // 收货人
        orderInfoEntity.setShipName(erpOrderEntity.getShipName());
        // 收货区域id
        orderInfoEntity.setShipAreaId(erpOrderEntity.getShipAreaId());
        // 收货地区
        orderInfoEntity.setShipArea(erpOrderEntity.getShipArea());
        // 收货地址
        orderInfoEntity.setShipAddr(erpOrderEntity.getShipAddr());
        // 收货人邮编
        orderInfoEntity.setShipZip(erpOrderEntity.getShipZip());
        // 收货电话
        orderInfoEntity.setShipTel(erpOrderEntity.getShipTel());
        // 配送时间
        orderInfoEntity.setShipTime(erpOrderEntity.getShipTel());
        // 收货人手机
        orderInfoEntity.setShipMobile(erpOrderEntity.getShipMobile());
        // 推荐码
        orderInfoEntity.setReferralsCode(erpOrderEntity.getReferralsCode());
        // 客户留言
        orderInfoEntity.setMemo(erpOrderEntity.getMemo());
        return orderInfoEntity;
    }

    /**
     * 紫云网订单商品明细==》erp订单商品明细
     * @param
     * @param
     * @return
     */
    public static OrderItemEntity erpToZiyunOrderItem(ErpOrderItemEntity erpOrderItemEntity) {
        OrderItemEntity orderItemEntity = new OrderItemEntity();
        orderItemEntity.setOrderId(Long.valueOf(erpOrderItemEntity.getOrderNo()));
        orderItemEntity.setItemId(erpOrderItemEntity.getOriginalItemId());
        orderItemEntity.setGoodsId(erpOrderItemEntity.getGoodsId().longValue());
        orderItemEntity.setProductId(erpOrderItemEntity.getProductId());
        orderItemEntity.setName( erpOrderItemEntity.getName());
        orderItemEntity.setPrice( erpOrderItemEntity.getPrice());
        orderItemEntity.setAmount( erpOrderItemEntity.getAmount());
        orderItemEntity.setSharePrice( erpOrderItemEntity.getSharePrice());
        orderItemEntity.setWeight( erpOrderItemEntity.getWeight());
        orderItemEntity.setNums( erpOrderItemEntity.getNums());
        orderItemEntity.setUnit( erpOrderItemEntity.getUnit());
        orderItemEntity.setAddon( erpOrderItemEntity.getAddon());
        orderItemEntity.setItemType( erpOrderItemEntity.getItemType());
        orderItemEntity.setGoodsType(erpOrderItemEntity.getAddcartBuyType());
        orderItemEntity.setPaperUnit( erpOrderItemEntity.getPaperUnit());
        orderItemEntity.setPaperSize( erpOrderItemEntity.getPaperSize());
        orderItemEntity.setCalculationPriceProcess( erpOrderItemEntity.getCalculationPriceProcess());
        orderItemEntity.setAttributeOption(erpOrderItemEntity.getAttributeOption());
        orderItemEntity.setQuoteSelfDefineParam(erpOrderItemEntity.getQuoteSelfDefineParam());
        orderItemEntity.setCutModel(erpOrderItemEntity.getCutModel());
        orderItemEntity.setDeleteFlag(erpOrderItemEntity.getItemDel());
        return orderItemEntity;
    }


}
